package top.itlir.edc.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.itlir.edc.dao.TransitInfoRepository;
import top.itlir.edc.dao.WayBillRepository;
import top.itlir.edc.domain.TransitInfo;
import top.itlir.edc.domain.WayBill;
import top.itlir.edc.index.WayBillIndexRepository;
import top.itlir.edc.service.ITransitInfoService;

@Service
@Transactional
public class TransitInfoServiceImpl implements ITransitInfoService {
	
	@Autowired
	private WayBillRepository wayBillRepository;
	
	@Autowired
	private WayBillIndexRepository wayBillIndexRepository;
	
	@Autowired
	private TransitInfoRepository transitInfoRepository;

	@Override
	public void startTransit(String waybillIds) {
		// 分割ids
		String[] idsArr = waybillIds.split(",");
		for (String uid : idsArr) {
			// 根据id查询运单信息
			WayBill wayBill = wayBillRepository.findOne(Integer.valueOf(uid));
			Integer signStatus = wayBill.getSignStatus();
			
			if(null != signStatus && 1 == signStatus){
				//3.是待发货，
				//3.1更新运单状态2-派送中
				wayBill.setSignStatus(2);
				//3.2更新ES索引服务器
				wayBillIndexRepository.save(wayBill);
				//3.3创建并保存运输配送对象信息
				TransitInfo transit = new TransitInfo();
				transit.setStatus("出入库中转");//运输配送状态，未到达最终网点之前，都是出入库中转
				transit.setWayBill(wayBill);//关联运输配送信息和运单信息
				transitInfoRepository.save(transit);
			}
			
		}
		
	}

	@Override
	public Page<TransitInfo> findAll(Specification<TransitInfo> spec,
			Pageable pageable) {
		return transitInfoRepository.findAll(spec, pageable);
	}

}
